<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class products_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        if(!$this->is_admin_logged()) redirect(base_url(). $this->base_admin_url .'login_controller');
        $this->page_title = 'Sản phẩm';
        $this->table_name = 'products';
        $this->route_name = 'products_controller';
        $this->business_model = $this->products_model;
    }

    public function index()
    {
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['list'] = $this->get_list();
        $view_data['actions'] = $this->get_list_actions();
        $this->load_view('list_template',$header_data,$view_data);
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        foreach($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/'.$item->id
            );
            $list[] = array(
                'can_delete' => $item->id,
                'id' => $item->id,
                'name' => $item->name,
                'unit' => $item->unit,
                'price' => number_format($item->price),
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'ID';
        $list[] = 'Tên món ăn';
        $list[] = 'Đơn vị tính';
        $list[] = 'Giá';
        $list[] = 'Action';
        return $list;

    }

    public function get_post_data()
    {

        $post_data = array(

            'name' => $this->input->post('name'),
            'unit' => $this->input->post('unit'),
            'price' => $this->input->post('price'),
        );
        return $post_data;

    }

    public function get_fields($id)
    {

        if($id == -1)
            $model = new $this->business_model();
        else
            $model = $this->business_model->get_element_by_id($id);

        $fields = array();

        $fields[] = array(
            'text' => 'Tên món',
            'name' => 'name',
            'value' => $model->name,
            'type' => 'text'
        );

        $fields[] = array(
            'text' => 'Đơn vị tính',
            'name' => 'unit',
            'value' => $model->unit,
            'type' => 'select',
            'options' => array(
                array_to_object(array(
                    'text' => 'Cái',
                    'value' => 'Cái'
                )),
                array_to_object(array(
                    'text' => 'Dĩa',
                    'value' => 'Dĩa'
                )),
                array_to_object(array(
                    'text' => 'Chai',
                    'value' => 'Chai'
                )),
                array_to_object(array(
                    'text' => 'Lon',
                    'value' => 'Lon'
                )),
                array_to_object(array(
                    'text' => 'Cặp',
                    'value' => 'Cặp'
                )),
                array_to_object(array(
                    'text' => 'Khung',
                    'value' => 'Khung'
                ))
            ),
            'select_value' => 'text',
            'select_display' => 'value'
        );

        $fields[] = array(
            'text' => 'Giá',
            'name' => 'price',
            'value' => $model->price,
            'type' => 'text'
        );

        return $fields;
    }

}
